Add night surplus forecast to battery state publishing#372
Merged
Conversation
…ction start Calculates how much battery energy (above MIN_SOC) will be left over between the end of today's solar production window and the start of tomorrow's. A positive value means the battery won't be fully discharged overnight and the surplus could drive flexible loads (heat pump, EV, etc.) before dawn. - core.py: new _compute_night_surplus() method; publishes via mqtt_api - mqtt_api.py: publish_night_surplus(), HA discovery sensor for /night_surplus_wh - tests/batcontrol/test_night_surplus.py: 10 unit tests covering edge cases https://claude.ai/code/session_01AbZuZ73iEQMiwTkwy3Mt1h
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new forecast-derived metric (“night surplus”) to estimate remaining usable battery energy after an overnight discharge period, and publishes it via MQTT (including Home Assistant discovery), with a dedicated pytest module.
Changes:
- Added
Batcontrol._compute_night_surplus()and integrated it into therun()loop to compute and publish the metric. - Extended
MqttApiwith/night_surplus_whpublishing and Home Assistant MQTT discovery configuration. - Added
tests/batcontrol/test_night_surplus.pyto validate the new computation across multiple forecast scenarios.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/batcontrol/core.py |
Adds computation for night surplus and publishes it during the main control loop. |
src/batcontrol/mqtt_api.py |
Introduces MQTT publish method + HA discovery entry for /night_surplus_wh. |
tests/batcontrol/test_night_surplus.py |
New tests covering the night surplus calculation behavior. |
Explains why net_delta intentionally spans [0:end_idx] (bridge + solar charging): stopping at production_start would give battery level at production start, not at production end, which is the wrong baseline for the overnight calculation. Also documents the no-second-window edge case (forecast horizon used as proxy). https://claude.ai/code/session_01AbZuZ73iEQMiwTkwy3Mt1h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds computation and MQTT publishing of the expected battery surplus at the start of the next solar production window (morning). This metric helps identify when the battery will retain usable charge after overnight discharge, enabling flexible load scheduling before dawn.
Changes
New method
_compute_night_surplus()incore.py: Calculates expected battery energy remaining after the overnight discharge period between end of today's solar production and start of tomorrow's production. Returns 0 if no solar production window exists in forecast.Integration in
run()loop: Calls_compute_night_surplus()with current production/consumption forecast and battery state, publishes result via MQTTMQTT API enhancements:
publish_night_surplus()method to publish/night_surplus_whtopicComprehensive test suite (
test_night_surplus.py): 13 test cases covering:Implementation Details
https://claude.ai/code/session_01AbZuZ73iEQMiwTkwy3Mt1h